home *** CD-ROM | disk | FTP | other *** search
- @echo off
- ! unixopt.bat Batch file to parse a UNIX-like option
- !
- ! An option as parsed by unixopt.bat consists of (from left to right):
- ! a dash as first character
- ! a character which identifies the option
- ! a string
- !
- ! Call unixopt as follows:
- !
- ! unixopt what err opt rest
- !
- ! where: what = string to be parsed
- ! err = name of the variable into which unixopt is to return an error code
- ! opt = name of the variable into which unixopt is to return the option
- ! character
- ! rest = name of the variable into which unixopt is to return the rest of
- ! 'what' after removing dash and option
- !
- ! unixopt returns into 'err' the following codes:
- ! 0 = no error
- ! 1 = 'what' consists of a dash and an option character without any additional
- ! string. In this case, unixopt sets 'opt' correctly and 'rest' to a single
- ! space.
- ! -1 = 'what' does not begin with a dash
- ! -2 = 'what' consists of a dash without any option character.
- ! -3 = unixopt was called with 2 or 3 parameters instead of 4.
- !
- ! If unixopt is called with less than 2 parameters, it returns without any action.
- !
- ! Copyright © 1993 by Rainbow Hill Pty Ltd. All rights reserved.
- !
-
- ! check for the presence of the parameters
- if "%1x" == "x" goto RET_LBL
- if "%2x" == "x" goto RET_LBL
- if "%3x" == "x" goto NOPAR_LBL
- if "%4x" == "x" goto NOPAR_LBL
-
- ! check for the presence of the dash at the beginning of par 1
- ! The 'x' is needed to ensure that 'tmp1' is not numeric and that it consists of
- ! at least two characters, so that 'decr' provides predictable results.
- set tmp1=%1x
- ! let's remove the first character (ie. the dash) from 'tmp1'
- decr -tmp1
- ! and now we remove the "dashless" first parameter from a full copy of it, so that
- ! we are left with the dash itself
- ! The 'y' is needed to prevent 'decr' from removing 'tmpt2' when the dash is not
- ! followed by any character.
- set tmp2=y%1x
- decr tmp2 by %tmp1%
- if not %tmp2% == y- goto NODASH_LBL
-
- ! check for the presence of something after the dash
- if %tmp1% == x goto NOOPT_LBL
-
- ! isolate the option character
- ! Now 'tmp1' contains at least one character (ie. the option character) followed
- ! by the 'x' that we have appended to %1 when we created 'tmp1'.
- set tmp2=%tmp1%
- !let's remove the first character (ie. the option character) from 'tmp1'
- decr -tmp1
- ! here we do what we did to isolate the dash, but we do not need to prepend a 'y'
- ! because we know that 'tmp2' contains at least two characters and 'tmp1' one
- decr tmp2 by %tmp1%
- ! we are now ready to return the option character
- set %3=%tmp2%
-
- ! check for the presence of something after the option character
- if %tmp1% == x goto NOREST_LBL
-
- ! there is still something left
- ! let's get rid of the 'x' and return the rest
- decr tmp1
- set %4=%tmp1%
-
- !return successfully
- set %2=0
- goto RET_LBL
-
- :NOREST_LBL
- set %2=1
- goto RET_LBL
- :NODASH_LBL
- set %2=-1
- goto RET_LBL
- :NOOPT_LBL
- set %2=-2
- goto RET_LBL
- :NOPAR_LBL
- set %2=-3
- :RET_LBL
- set tmp1=
- set tmp2=
-